package org.hxzon.demo.kafka0102;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

public class MyMessageClient {

	public static void main(String[] args) throws Exception {
		// Parse options.
		String host = "127.0.0.1";
		int port = 8080;
		// Configure the client.
		ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
		// Set up the event pipeline factory.
		bootstrap.setPipelineFactory(new MyMessageClientPipelineFactory());
		// Start the connection attempt.
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
		//add by hxzon
		final MyMessage msg = new MyMessage("学中");
		future.addListener(new ChannelFutureListener() {
			@Override
			public void operationComplete(ChannelFuture future) throws Exception {
				future.awaitUninterruptibly().getChannel().getPipeline().getContext(MyMessageClientHandler.class).setAttachment(msg);
			}
		});
		//end add
		// Wait until the connection is closed or the connection attempt fails.
		ChannelFuture closeFuture=future.getChannel().getCloseFuture();
		closeFuture.awaitUninterruptibly();
		// Shut down thread pools to exit.
		bootstrap.releaseExternalResources();
		//add by hxzon
		System.out.println(msg.getRes());
		//end add

	}
}
